package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Timer;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TGoodsInorders;
import com.ruoyi.system.mapper.TOrderDetailMapper;
import com.ruoyi.system.domain.TOrderDetail;
import com.ruoyi.system.service.ITOrderDetailService;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-29
 */
@Service
public class TOrderDetailServiceImpl implements ITOrderDetailService 
{
    @Autowired
    private TOrderDetailMapper tOrderDetailMapper;

    /**
     * 查询订单信息
     * 
     * @param orderId 订单信息主键
     * @return 订单信息
     */
    @Override
    public TOrderDetail selectTOrderDetailByOrderId(String orderId)
    {
        return tOrderDetailMapper.selectTOrderDetailByOrderId(orderId);
    }

    /**
     * 查询订单信息列表
     * 
     * @param tOrderDetail 订单信息
     * @return 订单信息
     */
    @Override
    public List<TOrderDetail> selectTOrderDetailList(TOrderDetail tOrderDetail)
    {
        return tOrderDetailMapper.selectTOrderDetailList(tOrderDetail);
    }

    /**
     * 新增订单信息
     * 
     * @param tOrderDetail 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public String insertTOrderDetail(TOrderDetail tOrderDetail)
    {
        tOrderDetail.setCreateTime(DateUtils.getNowDate());
        tOrderDetail.setOrderId(tOrderDetail.getPurchaseId()+'`'+String.valueOf(System.currentTimeMillis()));
        tOrderDetail.setOrderNo(tOrderDetail.getPurchaseId()+'`'+String.valueOf(System.currentTimeMillis()));
        int rows = tOrderDetailMapper.insertTOrderDetail(tOrderDetail);
        //insertTGoodsInorders(tOrderDetail);
        return tOrderDetail.getOrderId();
    }

    /**
     * 修改订单信息
     * 
     * @param tOrderDetail 订单信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTOrderDetail(TOrderDetail tOrderDetail)
    {
        tOrderDetail.setUpdateTime(DateUtils.getNowDate());
        tOrderDetailMapper.deleteTGoodsInordersByOrderId(tOrderDetail.getOrderId());
        insertTGoodsInorders(tOrderDetail);
        return tOrderDetailMapper.updateTOrderDetail(tOrderDetail);
    }

    /**
     * 批量删除订单信息
     * 
     * @param orderIds 需要删除的订单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTOrderDetailByOrderIds(String[] orderIds)
    {
        tOrderDetailMapper.deleteTGoodsInordersByOrderIds(orderIds);
        return tOrderDetailMapper.deleteTOrderDetailByOrderIds(orderIds);
    }

    /**
     * 删除订单信息信息
     * 
     * @param orderId 订单信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTOrderDetailByOrderId(String orderId)
    {
        tOrderDetailMapper.deleteTGoodsInordersByOrderId(orderId);
        return tOrderDetailMapper.deleteTOrderDetailByOrderId(orderId);
    }

    /**
     * 新增订单商品信息信息
     * 
     * @param tOrderDetail 订单信息对象
     */
    public void insertTGoodsInorders(TOrderDetail tOrderDetail)
    {
        List<TGoodsInorders> tGoodsInordersList = tOrderDetail.getTGoodsInordersList();
        String orderId = tOrderDetail.getOrderId();
        if (StringUtils.isNotNull(tGoodsInordersList))
        {
            List<TGoodsInorders> list = new ArrayList<TGoodsInorders>();
            for (TGoodsInorders tGoodsInorders : tGoodsInordersList)
            {
                tGoodsInorders.setOrderId(orderId);
                list.add(tGoodsInorders);
            }
            if (list.size() > 0)
            {
                tOrderDetailMapper.batchTGoodsInorders(list);
            }
        }
    }
    public int insertTOrderInfo(List<TGoodsInorders> list){
        return tOrderDetailMapper.batchTGoodsInorders(list);
    }
}
